package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysRole;
import cn.wgx.modules.sys.entity.SysRolePermission;
import cn.wgx.modules.sys.services.SysPermissionService;
import cn.wgx.modules.sys.services.SysRolePermissionService;
import cn.wgx.modules.sys.services.SysRoleService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "3.1 权限分组 - 角色 ")
@RestController
@RequestMapping(value="${allPathPre}sysrole")
public class SysRoleController extends BaseController {

    @Resource
    SysRoleService sysRoleService;

    @Resource
    SysRolePermissionService sysRolePermissionService;

    @Resource
    SysPermissionService sysPermissionService;

    @ApiOperation("获取角色列表，roleName参支持模糊查询")
    @ApiPermission(value = false)
    @RequestMapping(path = "list", method = RequestMethod.GET)
    public Map<String,Object> getList(@ModelAttribute SysRole record
            , @RequestParam(value = "page", defaultValue = "1") Integer page
            , @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize
            , @ApiParam("选择排序时必填根据哪个字段排序") @RequestParam(required = false) String title
            , @ApiParam("desc或asc排序") @RequestParam(required = false) String order) {
        return returnData(PageHelper.startPage(page, pageSize), sysRoleService.getList(record,title,order));
    }

    @ApiOperation("获取单个角色详细信息")
    @ApiPermission(value = false)
    @RequestMapping(path = "get", method = RequestMethod.GET)
    public Map<String,Object> getOne(Integer role_id) {
        return returnData(sysRoleService.getOne(role_id));
    }


    @ApiOperation("根据角色ID获取权限列表")
    @ApiPermission(value = false)
    @RequestMapping(path = "getPerByRoleId", method = RequestMethod.GET)
    public Map<String,Object> getPer(Integer role_id) {
        SysRolePermission sysRolePermission = new SysRolePermission();
        sysRolePermission.setRole_permission_id(role_id);
        return returnData(sysRolePermissionService.getList(sysRolePermission));
    }


    @ApiOperation("新建角色")
    @ApiPermission
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String,Object> insertOne(@RequestBody SysRole record) {
       List<SysRole> lists = sysRoleService.getList(record,"-1",null);    //title 精确查询
        for (SysRole sysRole:lists) {
            if(StringUtils.isNotEmpty(sysRole.getRoleKey()) && sysRole.getRoleKey().equals(record.getRoleKey())){
                return returnErrorMap("角色别名重复");
            }
        }
        return returnData(sysRoleService.insertOne(record));
    }

    @ApiOperation("修改角色信息")
    @ApiPermission
    @RequestMapping(path = "update", method = RequestMethod.PATCH)
    public Map<String,Object> updateOne(@RequestBody SysRole record) {
        List<SysRole> lists = sysRoleService.getList(record,"-1",null);   //title -1精确查询
        for (SysRole sysRole:lists) {
            if(!sysRole.getRole_id().equals(record.getRole_id()) && StringUtils.isNotEmpty(sysRole.getRoleKey()) && sysRole.getRoleKey().equals(record.getRoleKey())){
                return returnErrorMap("角色别名重复");
            }
        }
        return returnData(sysRoleService.updateOne(record));
    }

    @ApiOperation("角色授权 -> 添加/删除权限, 一对多")
    @ApiPermission
    @RequestMapping(path = "updatePermission", method = RequestMethod.PATCH)
    public Map<String,Object> updateRolePermission(@ApiParam(value = "SysRolePermission对象数组,只需roleId和permissionId") @RequestBody List<SysRolePermission> sysRolePermissions) {
        if(sysRolePermissions == null || sysRolePermissions.size() == 0){
            return returnErrorMap("参数错误");
        }else {
//            if(sysRolePermissions.get(0).getRoleId() == 1){
//                return returnErrorMap("管理员角色不能修改.");
//            }
        }
        //todo 动态更新权限
        Integer n = sysRolePermissionService.updateRolePermission(sysRolePermissions);
        if(n > 0){
            sysPermissionService.clearListCache();
        }
        return returnData(n);
    }

    @ApiOperation("删除角色")
    @ApiPermission
    @RequestMapping(path = "del/{id}", method = RequestMethod.DELETE)
    public Map<String,Object> deleteOne(@PathVariable("id") Integer id) {
        if(id <= 5){
            return returnErrorMap("固定角色不能删除");
        }
        int o = sysRoleService.deleteOne(id);
        if(o > 0){
            sysRolePermissionService.deleteByMenuId(id);
        }
        return returnData(o);
    }


}